package com.huawei.fakedouyin.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;

import com.huawei.fakedouyin.R;
import com.huawei.fakedouyin.databinding.DialogDanmuSendBinding;
import com.huawei.fakedouyin.databinding.DialogDanmuSwitchBinding;
import com.huawei.fakedouyin.manager.PlayManager;
import com.huawei.fakedouyin.util.ActivityUtil;
import com.huawei.fakedouyin.util.LiveDataSet;
import com.huawei.fakedouyin.util.LogUtil;
import com.huawei.fakedouyin.util.SPUtil;
import com.huawei.fakedouyin.util.ScreenUtilKt;
import com.huawei.fakedouyin.util.ViewUtil;
import com.huawei.faketaobao.constant.DMSettingText;
import com.huawei.faketaobao.constant.IConstant;
import com.huawei.faketaobao.constant.SPConstant;

import java.math.BigInteger;


/**
 * 弹幕开关弹框 弹幕设置弹框入口
 */
public class DanmuSwitchDialog extends AppCompatDialog {

    DialogDanmuSwitchBinding binding;
    private int size = 1;

    public DanmuSwitchDialog(@NonNull Context context) {
        super(context, R.style.BaseDialogTheme);
        initView();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setGravity(Gravity.BOTTOM);
        ViewGroup.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = ScreenUtilKt.screenWidth();
    }

    private void initView() {
        binding = DialogDanmuSwitchBinding.inflate(getLayoutInflater(), null, false);
        setContentView(binding.getRoot());
        //圆形图片
        ViewUtil.setViewOutlineProviderYuan(binding.photo1);
        ViewUtil.setViewOutlineProviderYuan(binding.photo2);
        ViewUtil.setViewOutlineProviderYuan(binding.photo3);
        ViewUtil.setViewOutlineProviderYuan(binding.photoSmall);
        refresh();
        initClick();

    }

    /**
     * 刷新弹幕开关
     */
    private void refresh() {
        binding.danmuSwitchIcon.setImageDrawable(
                SPUtil.Companion.getInstance().getBoolean
                        (SPConstant.danmuSwitch, IConstant.danmuSwitch) ?
                        getContext().getDrawable(R.drawable.switch_on) :
                        getContext().getDrawable(R.drawable.switch_off));

        if (SPUtil.Companion.getInstance().getBoolean
                (SPConstant.danmuSwitch, IConstant.danmuSwitch)) {
            binding.dnamuSetLn.setVisibility(View.VISIBLE);
        } else {
            binding.dnamuSetLn.setVisibility(View.GONE);
        }


    }

    private void initClick() {

        //弹幕设置 速度、字体、透明度等
        binding.dnamuSetLn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DanmuSetDialog(ActivityUtil.Companion.getInstance().getCurrentActivity()).show();
                dismiss();
            }
        });

        //弹幕开关按钮
        binding.danmuSwitchIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean now = SPUtil.Companion.getInstance().getBoolean
                        (SPConstant.danmuSwitch, IConstant.danmuSwitch);
                SPUtil.Companion.getInstance().put(SPConstant.danmuSwitch, !now);

                //刷新ui 通知更新
                if (now) {
                    LiveDataSet.danmuSwitch.postValue(!now);
                    dismiss();
                    Toast.makeText(getContext(), "弹幕已关闭", Toast.LENGTH_SHORT).show();
                } else {
                    LiveDataSet.danmuSwitch.postValue(!now);
                    refresh();
                    Toast.makeText(getContext(), "弹幕已开启", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }


}
